package com.ims.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ims.pojo.Menu;
import com.ims.pojo.User;
import com.ims.service.MenuService;
import com.ims.service.UserService;
import com.ims.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @author: zk
 * @date: 2022/9/8 18:13
 */
@Controller
public class ViewController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    private static String loginUrl = "login";

    @GetMapping("/")
    public String showLogin() {
        return loginUrl;
    }

    @GetMapping("/ims")
    public String showLogin2() {
        return loginUrl;
    }

    @GetMapping("/ims/login")
    public String showLogin3() {
        return loginUrl;
    }

    @GetMapping("/ims/index")
    public String showIndex(@RequestParam(value = "userId") Integer userId, ModelMap mmap) {
        Jedis jedis = new Jedis(RedisUtil.host, RedisUtil.port);
        String username = jedis.get("username" + userId);
        jedis.close();
        if (!StringUtils.hasText(username)) {
            return "redirect:/";
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        User user = userService.getOne(wrapper);
        List<Menu> menuList = menuService.selectMenuListById(user.getUserId());
        mmap.put("menus", menuList);
        mmap.put("username", user.getUsername());
        mmap.put("phone", user.getPhone().substring(7));
        return "index";
    }



    // 显示首页内容
    @GetMapping("/ims/main")
    public String showMainPage() {
        return "main";
    }

    @GetMapping("/user/show")
    public String showUserPage() {
        return "user/user";
    }

    @GetMapping("/role/show")
    public String showRolePage() {
        return "role/role";
    }

    @GetMapping("/monitor/device")
    public String showDevicePage() {
        return "monitor/device";
    }

    @GetMapping("/monitor/status")
    public String showStatusPage() {
        return "monitor/status";
    }

    @GetMapping("/log/userLog")
    public String showUserLog() {
        return "log/userLog";
    }
}
